package job

const (
	// 父作业状态码
	JobSuccessCode     = "3001"
	JobProcessingCode  = "3002"
	JobRunningCode     = "3003"
	JobDiscontinueCode = "3004"
	JobNotRunCode      = "3005"
	JobFailedCode      = "3006"
	JobPartSuccessCode = "3007"
	// 父作业
	JobSuccess     = "执行成功"
	JobProcessing  = "待处理"
	JobRunning     = "执行中"
	JobDiscontinue = "执行终止"
	JobNotRun      = "未执行"
	JobFailed      = "执行异常"
	JobPartSuccess = "部分成功"

	// 子作业状态码
	SubJobSuccessCode    = "30010"
	SubJobProcessingCode = "30020"
	SubJobRunningCode    = "30030"
	SubJobNotRunCode     = "30040"
	SubJobFailedCode     = "30050"
	SubJobTimeoutCode    = "30060"
	SubJobCanceledCode   = "30070"
	// 子作业
	SubJobSuccess    = "执行成功"
	SubJobProcessing = "待处理"
	SubJobRunning    = "执行中"
	SubJobNotRun     = "未执行"
	SubJobFailed     = "执行异常"
	SubJobTimeout    = "超时终止"
	SubJobCanceled   = "已取消"

	// 作业类型
	JobTypeManual = "手动执行"
	JobTypeCron   = "定时执行"
)

// 获取作业状态
func JobStatusCodeInit(jobType string) string {
	switch jobType {
	case JobTypeManual:
		return JobRunningCode // 手动执行 → 执行中
	case JobTypeCron:
		return JobNotRunCode // 定时执行 → 未执行
	default:
		return JobRunningCode
	}
}

// 获取子作业状态
func JobSubStatusCodeInit(jobType string, stepNum int) string {
	switch jobType {
	case JobTypeManual:
		if stepNum == 1 {
			return SubJobRunningCode // 第一步 → 执行中
		}
		return SubJobNotRunCode // 其他步骤 → 未执行
	case JobTypeCron:
		return SubJobNotRunCode // 定时执行 → 未执行
	default:
		return SubJobNotRunCode
	}
}

var (
	JobStatusMap = map[string]string{
		JobNotRunCode:      JobNotRun,
		JobProcessingCode:  JobProcessing,
		JobRunningCode:     JobRunning,
		JobDiscontinueCode: JobDiscontinue,
		JobSuccessCode:     JobSuccess,
		JobFailedCode:      JobFailed,
		JobPartSuccessCode: JobPartSuccess,
	}

	SubJobStatusMap = map[string]string{
		SubJobNotRunCode:     SubJobNotRun,
		SubJobProcessingCode: SubJobProcessing,
		SubJobRunningCode:    SubJobRunning,
		SubJobSuccessCode:    SubJobSuccess,
		SubJobFailedCode:     SubJobFailed,
		SubJobTimeoutCode:    SubJobTimeout,
		SubJobCanceledCode:   SubJobCanceled,
	}
)

func GetJobStatusByCode(code string) string {
	if status, ok := JobStatusMap[code]; ok {
		return status
	}
	return "未知状态"
}

// GetSubJobStatusByCode 通过状态码获取子作业中文状态
func GetSubJobStatusByCode(code string) string {
	if status, ok := SubJobStatusMap[code]; ok {
		return status
	}
	return "未知状态"
}
